package com.gzsxy.sso.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.sso.common.dto.request.AppRequest;
import com.gzsxy.sso.common.result.R;
import com.gzsxy.sso.core.entity.App;
import com.gzsxy.sso.core.entity.Function;
import com.gzsxy.sso.core.entity.vo.AppQueryVo;
import com.gzsxy.sso.core.entity.vo.RoleQueryVo;
import com.gzsxy.sso.core.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 应用管理
 * @date 2021/11/18 11:28
 */
@Api(description = "应用管理")
@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/admin/app")
public class AppController {


    @Autowired
    private AppService appService;

    @ApiOperation("获取所有的应用信息")
    @GetMapping("/getAll")
    public R getAllApp(){
        List<App> list = appService.getAllApp();
        return R.ok().message("查询成功").data("data",list);
    }

    @ApiOperation("分页查询应用列表")
    @GetMapping("/list")
    public R searchPageApp(@ApiParam(value = "当前页面",required = true,defaultValue ="1" ) @RequestParam Long page,         //long数据类型适用于较大整数类型
                  @ApiParam(value = "每页记录数",required = true,defaultValue ="5") @RequestParam Long limit,
                  @ApiParam("查询对象") AppQueryVo appQueryVo){
        Page<App> pageParam = new Page<>(page,limit);

        IPage<App> appIPage = appService.searchPageApp(pageParam,appQueryVo);
        List<App> records = appIPage.getRecords();
        //总记录数
        long total = appIPage.getTotal();
        return R.ok().message("查询成功").data("total",total).data("row",records);
    }


    @ApiOperation("新增应用")
    @PostMapping("/add")
    public R addApp(@RequestBody AppRequest appRequest){
        appService.addApp(appRequest);
        return R.ok().message("新增成功");
    }


    @ApiOperation("编辑应用")
    @PutMapping("/update")
    public R updateApp(@RequestBody AppRequest appRequest){
        appService.updateApp(appRequest);
        return R.ok().message("更新成功");
    }


    @ApiOperation("删除应用")
    @DeleteMapping("/delete/{appId}")
    public R deleteApp(@PathVariable String appId){
        appService.deleteApp(appId);
        return R.ok().message("删除成功");
    }

    @ApiOperation("根据id查询应用")
    @GetMapping("/{appId}")
    public R selectById(@ApiParam(value = "应用id",required = true)
                        @PathVariable String appId){
        App app = appService.selectById(appId);
        return R.ok().message("查询成功").data("data",app);
    }


}
